// 引入创建pinia的函数
import {defineStore} from "pinia"
// 引入发送请求的函数
import {reqCategory1ListData,reqGetCategory2ListData,reqGetCategory3ListData} from "@/api/product/category";
// import { log } from "console";
import type {CategoryStateModel} from "@/api/product/model/categoryData"


export const useCategoryStore = defineStore('CategoryList',{
    state: ():CategoryStateModel=>{
     return{
      // 存储三级导航id
      category1Id: undefined ,
      category2Id: undefined,
      category3Id: undefined,


      // 存储三级导航下拉框数据
      category1List: [],
      category2List: [],
      category3List: [],

     }
    },
    actions:{
        // 发送请求获取三级导航中的一级导航中的数据
       async getCategory1ListData(){
         const result = await reqCategory1ListData()
        //  console.log(result);
         this.category1List = result;
         
        },
        // 当一级下拉框中属性改变时触发 请求二级导航数据
        async getCategory2ListData(id:number) {
          const result = await reqGetCategory2ListData(id)
          this.category2List = result;
          this.category3List = [];
          // 将一级id传件来
          this.category1Id = id
          this.category2Id = undefined
          this.category3Id = undefined
        },
        // 获取二级id,三级列表数据
        async getCategory3ListData(id:number) {
          const result = await reqGetCategory3ListData(id)
          this.category3List = result;
          // 将一级id传件来
          this.category2Id = id
           // 重置三级id
          this.category3Id = undefined
        },
        // 将三级导航id穿过来
        saveCategory3Id(id: number){
          this.category3Id = id
        }
    }
})